package com.example.wendao.view.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.adapterdataobserverproxy.AdapterDataObserverProxy;
import com.example.wendao.view.viewholder.AgreeForumViewHolder;
import com.example.wendao.view.viewholder.FooterViewHolder;
import com.example.wendao.view.viewholder.AgreeForumViewHolder;
import com.example.wendao.view.viewholder.HeaderViewHolder;
import com.example.wendao.view.viewholder.UserViewHolder;
import com.example.wendao.viewmodel.ForumViewModel;


public class AgreementForumListAdapter extends PagedListAdapter<Forum, RecyclerView.ViewHolder> {

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
      if (viewType == PageUtils.ITEM_TYPE_FOOTER){
            viewHolder = new FooterViewHolder(parent);
        } else {
            viewHolder = new AgreeForumViewHolder(parent);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  FooterViewHolder){
            ((FooterViewHolder) holder).bindToFoot();
        } else {
            ((AgreeForumViewHolder) holder).bindTo(getForumItem(position));
        }
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() + 1;
    }

    private Forum getForumItem(int position){
        return getItem(position);
    }

    @Override
    public int getItemViewType(int position) {
        int type;
        int itemCount = getItemCount() - 1;
        if (position == itemCount){
            type = PageUtils.ITEM_TYPE_FOOTER;
        } else {
            type = super.getItemViewType(position);
        }
        return type;
    }
    @Override
    public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(new AdapterDataObserverProxy(observer, 1));
    }

    private static DiffUtil.ItemCallback<Forum> DIFF_CALLBACK = new DiffUtil.ItemCallback<Forum>() {


        @Override
        public boolean areItemsTheSame(@NonNull Forum oldForum, @NonNull Forum newForum) {
            return oldForum.getForumId() == newForum.getForumId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Forum oldForum, @NonNull Forum newForum) {
            return oldForum.equals(newForum);
        }
    };
    public AgreementForumListAdapter() {
        super(DIFF_CALLBACK);
    }
}
